package com.artisan.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.artisan.dao.CourseDao;
import com.artisan.dao.ScoreDao;
import com.artisan.dao.SelectedCourseDao;
import com.artisan.dao.StudentDao;
import com.artisan.model.Course;
import com.artisan.model.Score;
import com.artisan.model.Student;
import com.artisan.model.Teacher;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;

public class AddScoreFrm extends JInternalFrame {
	private JTextField scoreTextField;
	private JComboBox studentComboBox;
	private JComboBox courseComboBox;
	private JButton submitButton;
	private List<Course> courseList;
	private List<Student> studentList;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddScoreFrm frame = new AddScoreFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddScoreFrm() {
		setClosable(true);
		setIconifiable(true);

		getContentPane().setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JLabel lblNewLabel = new JLabel("学生姓名:");
		lblNewLabel.setIcon(new ImageIcon(AddScoreFrm.class.getResource("/com/artisan/image/学生管理.png")));
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		studentComboBox = new JComboBox();
		studentComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JLabel lblNewLabel_1 = new JLabel("课程信息:");
		lblNewLabel_1.setIcon(new ImageIcon(AddScoreFrm.class.getResource("/com/artisan/image/课程.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		courseComboBox = new JComboBox();
		courseComboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent ae) {
				courseChangeAct(ae);
			}
		});

		JLabel lblNewLabel_2 = new JLabel("所得成绩:");
		lblNewLabel_2.setIcon(new ImageIcon(AddScoreFrm.class.getResource("/com/artisan/image/成绩.png")));
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		scoreTextField = new JTextField();
		scoreTextField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		scoreTextField.setColumns(10);

		submitButton = new JButton("录入成绩");
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				submitAct(ae);
			}
		});
		submitButton.setIcon(new ImageIcon(AddScoreFrm.class.getResource("/com/artisan/image/确认.png")));
		submitButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addGap(92)
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
						.createSequentialGroup()
						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(lblNewLabel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblNewLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 87,
										Short.MAX_VALUE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(studentComboBox, GroupLayout.PREFERRED_SIZE, 184,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(courseComboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
						.addGroup(groupLayout.createSequentialGroup().addComponent(lblNewLabel_2)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(submitButton)
										.addComponent(scoreTextField, GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))))
				.addGap(167)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addGap(27)
				.addGroup(groupLayout
						.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel).addComponent(studentComboBox,
								GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1).addComponent(
						courseComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
						GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2).addComponent(
						scoreTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
						GroupLayout.PREFERRED_SIZE))
				.addGap(28).addComponent(submitButton).addContainerGap(53, Short.MAX_VALUE)));
		getContentPane().setLayout(groupLayout);
		setFrameIcon(new ImageIcon(AddScoreFrm.class.getResource("/com/artisan/image/成绩.png")));
		setTitle("成绩录入");
		setBounds(100, 100, 475, 275);
		setCourseCombox();
		setStudentCombox();
	}

	protected void submitAct(ActionEvent ae) {
		// TODO 自动生成的方法存根
		int score = 0;
		try {
			score = Integer.parseInt(scoreTextField.getText().toString());
		} catch (Exception e) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(this, "成绩必须输入大于0的整数!");
			return;
		}
		if (score <= 0) {
			JOptionPane.showMessageDialog(this, "成绩必须输入大于0的整数!");
			return;
		}
		Student student = (Student) studentComboBox.getSelectedItem();
		Course course = (Course) courseComboBox.getSelectedItem();
		Score scoreObj = new Score();
		scoreObj.setStudent_id(student.getId());
		scoreObj.setCourse_id(course.getId());
		scoreObj.setScore(score);
		ScoreDao scoreDao = new ScoreDao();
		if (scoreDao.isAdd(scoreObj)) {
			JOptionPane.showMessageDialog(this, "成绩已经录入，请勿重复录入!");
			return;
		}
		if (scoreDao.addScore(scoreObj)) {
			JOptionPane.showMessageDialog(this, "成绩录入成功!");
			scoreTextField.setText("");
		} else {
			JOptionPane.showMessageDialog(this, "成绩录入失败!");
		}
		scoreDao.closeDao();
	}

	protected void courseChangeAct(ItemEvent ae) {
		// TODO 自动生成的方法存根
		if (ae.getStateChange() == ItemEvent.SELECTED) {
			setStudentCombox();
		}
	}

	private void setCourseCombox() {
		CourseDao courseDao = new CourseDao();
		courseList = courseDao.getCourseList(new Course());
		courseDao.closeDao();
		for (Course course : courseList) {
			if ("教师".equals(MainFrm.userType.getName())) {
				Teacher teacher = (Teacher) MainFrm.userObject;
				if (course.getTeacher_id() == teacher.getId()) {
					courseComboBox.addItem(course);
				}
				continue;
			}
			// 执行到这里一定是管理员身份
			courseComboBox.addItem(course);
		}
	}

	private void setStudentCombox() {
		studentComboBox.removeAllItems();
		StudentDao studentDao = new StudentDao();
		studentList = studentDao.getStudentList(new Student());
		studentDao.closeDao();
		Course course = (Course) courseComboBox.getSelectedItem();
		List<Student> selectedCourseStudentList = getSelectedCourseStudentList(course);
		for (Student student : studentList) {
			for (Student student2 : selectedCourseStudentList) {
				if (student.getId() == student2.getId())
					studentComboBox.addItem(student);
			}
		}
	}

	private List<Student> getSelectedCourseStudentList(Course course) {
		SelectedCourseDao scDao = new SelectedCourseDao();
		List<Student> selectedCourseStudentList = scDao.getSelectedCourseStudentList(course);
		return selectedCourseStudentList;
	}
}
